package com.stedu.service.impl;

import com.stedu.bean.College;
import com.stedu.exception.SteduException;
import com.stedu.mapper.CollegeMapper;
import com.stedu.mapper.StudentMapper;
import com.stedu.service.CollegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class CollegeServiceImpl implements CollegeService {
    @Autowired
    private CollegeMapper collegeMapper;
    @Autowired
    private StudentMapper studentMapper;
    @CacheEvict(value = "select_class", key = "'collegeList'")
    @Override
    public boolean insert(College c) throws SteduException {
        //判断被添加的专业是否已经存在
        if(collegeMapper.selectByProfessional(c.getProfessional()) != null) {
            //抛出自定义异常
            throw new SteduException("该专业已经存在，无法重复添加");
        }
        return collegeMapper.insert(c) == 1;
    }

    @Caching(
            evict = {
                    @CacheEvict(value = "select_class", key = "'collegeList'"),
                    @CacheEvict(value = "select_class", key = "#zid")
            }
    )
    @Override
    public boolean delete(Integer zid) throws SteduException {
        if(!studentMapper.selectByZid(zid).isEmpty()){
            return false;
        }
        return collegeMapper.delete(zid) == 1;
    }

    @Caching(
            evict = {
                    @CacheEvict(value = "select_class", key = "'collegeList'"),
                    @CacheEvict(value = "select_class", key = "#c.zid")
            }
    )
    @Override
    public boolean update(College c) throws SteduException {
        if(collegeMapper.selectByZid(c.getZid()) == null) {
            throw new SteduException("该学院信息不存在，无法修改");
        }
        /*
         * 修改学院名字后，可以与其他学院名字相同，但是专业名字不能一样
         * */
        //根据修改之后的专业名字c.getProfessional()查询
        College college = collegeMapper.selectByProfessional(c.getProfessional());
        if(college != null && college.getProfessional() != c.getProfessional()) {
            throw new SteduException("修改之后的专业名字和已有专业名字重复，不允许修改");
        }
        return collegeMapper.update(c) == 1;
    }

    @Cacheable(value = "select_class", key = "'collegeList'", unless = "#result==null||#result.size()==0")
    @Override
    public List<College> selectAll() {
        return collegeMapper.selectAll();
    }

    @Cacheable(value = "select_class", key = "#zid", unless = "#result==null")
    @Override
    public College selectByZid(Integer zid) {
        return collegeMapper.selectByZid(zid);
    }

    @Override
    public College selectByFaculty(String faculty) {
        return collegeMapper.selectByFaculty(faculty);
    }

    @Override
    public College selectByProfessional(String professional) {
        return collegeMapper.selectByProfessional(professional);
    }
}
